const app = getApp()

Page({
  data: {
    content: "",
    description: "",
    todoItem: {},
    isEdit: false
  },
  handleClick() {
    if (!this.data.isEdit) {
      // 进入编辑状态
      this.setData({
        isEdit: true
      })
    } else {
      // 提交保存
      console.log(this.data.content)
      console.log(this.data.description)
      app.http({
        method: "POST",
        url: `https://api.yizhanketang.cn/api/v1/todos/${this.data.todoItem.objectId}`,
        data: {
          content: this.data.content,
          description: this.data.description
        }
      }).then(res => {
        console.log(res)
        this.setData({
          isEdit: false
        })
      })
      // ajax

    }

  },
  onLoad(query) {
    const self = this
    const { id } = query
    app.http({
      url: `https://api.yizhanketang.cn/api/v1/todos/${id}`
    }).then(res => {
      self.setData({
        todoItem: res.data.data,
        content: res.data.data.content,
        description: res.data.data.description,
      })
      // console.log(res.data)
    })
  }
})
